package com.cynteka.cyfoman.core.collection;

import com.cynteka.cyfoman.core.IValueObject;

import javax.sql.DataSource;
import java.util.Collection;
import java.util.List;

/**
 * User: Sergey
 * Date: 29.07.11
 * Time: 21:02
 */
public class DBTableReferenceAppender implements IRegistryAppender {
    private DataSource dataSource;
    private ISQLGenerator sqlGenerator = new BaseSQLGenerator();

    private String linkId;

    private String sourceCollectionId;
    private String destinationCollectionId;
    private String referenceFieldName;


    private String sourceTableName;
    private String sourceIdFieldName = "id";
    private String destinationTableName;
    private String destinationIdFieldName = "id";
    private LinkType linkType = LinkType.ONE_TO_MANY;


    public class Link extends BaseLink<IValueObject<Integer, String>, IValueObject<Integer, String>> {

        public Link() {
            super(linkId, sourceCollectionId, destinationCollectionId, linkType);
        }

        @Override
        public Collection<IValueObject<Integer, String>> getDestinationItems(IValueObject<Integer, String> valueObject) {
            initNames();
            String sql = sqlGenerator.generateSelectReferenceTable(destinationTableName, referenceFieldName);
            List<IValueObject<Integer, String>> res = new DBUtil<Integer>().queryForValueObjects(dataSource, sql, destinationIdFieldName, valueObject.getId());
            return res;
        }

        @Override
        public Collection<IValueObject<Integer, String>> getSourceItems(IValueObject<Integer, String> valueObject) {
            initNames();
            String sql = sqlGenerator.generateSelectByChildId(sourceTableName, sourceIdFieldName, destinationTableName, destinationIdFieldName, referenceFieldName);
            List<IValueObject<Integer, String>> res = new DBUtil<Integer>().queryForValueObjects(dataSource, sql, destinationIdFieldName, valueObject.getId());
            return res;
        }

        protected void initNames() {
            if (sourceTableName == null) {
                sourceTableName = DBUtil.getTableName(getOwner(), sourceCollectionId);
            }
            if (destinationTableName == null) {
                destinationTableName = DBUtil.getTableName(getOwner(), destinationCollectionId);
            }

            if (referenceFieldName == null) {
                referenceFieldName = DBUtil.generateReferenceFieldName(sourceTableName, destinationIdFieldName);
            }
        }
    }

    public String getSourceIdFieldName() {
        return sourceIdFieldName;
    }

    public void setSourceIdFieldName(String sourceIdFieldName) {
        this.sourceIdFieldName = sourceIdFieldName;
    }

    public String getDestinationTableName() {
        return destinationTableName;
    }

    public void setDestinationTableName(String destinationTableName) {
        this.destinationTableName = destinationTableName;
    }

    public String getDestinationIdFieldName() {
        return destinationIdFieldName;
    }

    public void setDestinationIdFieldName(String destinationIdFieldName) {
        this.destinationIdFieldName = destinationIdFieldName;
    }

    @Override
    public ICollection[] getCollections() {
        return new ICollection[0];
    }

    @Override
    public ILink[] getLinks() {
        return new ILink[]{new Link()};
    }

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public ISQLGenerator getSqlGenerator() {
        return sqlGenerator;
    }

    public void setSqlGenerator(ISQLGenerator sqlGenerator) {
        this.sqlGenerator = sqlGenerator;
    }

    public String getLinkId() {
        return linkId;
    }

    public void setLinkId(String linkId) {
        this.linkId = linkId;
    }

    public String getSourceCollectionId() {
        return sourceCollectionId;
    }

    public void setSourceCollectionId(String sourceCollectionId) {
        this.sourceCollectionId = sourceCollectionId;
    }

    public String getDestinationCollectionId() {
        return destinationCollectionId;
    }

    public void setDestinationCollectionId(String destinationCollectionId) {
        this.destinationCollectionId = destinationCollectionId;
    }

    public String getReferenceFieldName() {
        return referenceFieldName;
    }

    public void setReferenceFieldName(String referenceFieldName) {
        this.referenceFieldName = referenceFieldName;
    }

    public String getSourceTableName() {
        return sourceTableName;
    }

    public void setSourceTableName(String sourceTableName) {
        this.sourceTableName = sourceTableName;
    }

    public LinkType getLinkType() {
        return linkType;
    }

    public void setLinkType(LinkType linkType) {
        this.linkType = linkType;
    }


}
